<?php
include_once('app/itemLib.php');
//----recover stuff from DB---------
$itemInfo = recoverItemInfo(get('id'));
if($itemInfo['queryStatus'] == 'DB Query success!')
{
	$itemDesc = $itemInfo['itemDesc'][0];
	$itemCat = $itemInfo['category'][0];
	$itemnotes = $itemInfo['notes'][0];
	$disable = 'disabled';
	$acctBtn = 'Update Account';
	$formID = '&id='.get('id');
	$totalItemCount = countItems($itemCat);
	$deleteOption = getDeletedCategory();
	if($totalItemCount == 1)
	{
		$lastEntry = '&lastEntry=yes';
		$lastEntryMsg = 'This is the last entry for this category. Changing this into a new category will delete the previous category. Do you still want to continue?';
	}
	else
	{
		$lastEntry = '';
		$lastEntryMsg = '';
	}
	$itemInputBox = '<input type="text" id="itemID" name="itemID" value="'.$itemInfo['itemID'][0].'" disabled/> <input type="hidden" id="itemID" name="itemID" value="'.$itemInfo['itemID'][0].'" />';
}
else
{
	$itemID = '';
	$itemDesc = '';
	$itemCat = 'noMatch';
	$itemnotes = '';
	$disable = '';
	$acctBtn = 'Create Account';
	$formID = '';
	$lastEntry = '';
	$lastEntryMsg = '';
	$itemInputBox = '<input type="text" id="itemID" name="itemID"/>';
	$deleteOption = '';
}
$categories = recoverCategories();
$options = ($categories['queryStatus'] == 'DB Query success!' ? changeToOptions($categories['catID'], $categories['catName'], $itemCat).$deleteOption : '');

//----

if(post('process') == 'null')
{
	$display = '
	<form id="newItemFormID" method="POST" action="'.INDEX.'control'.EXT.'sector=newItem'.$formID.$lastEntry.'" rel=" '.$lastEntryMsg.'"><input type="hidden" name="process" value="nt" />
	<div class="grid_12 mainBox">
		<div class="grid_12 alpha">
			<h1>Item Account Properties</h1>
		</div>
		<div class="clear">
		<!--spanner-->
		</div>
		<div class="grid_6 alpha">
			<div class="grid_6 alpha bordered widgetDiv">
				<div class="grid_3 alpha bold">
					Item Code:<br />'.$itemInputBox.'<br />
					Category:<br />
					<select id="itemCat" name="category">
						<option value="---" selected>--Select a Category--</option>
						<option value="newCat">New Category*</option>'.$options.'
					</select>
					<input type="hidden" name="prevCat" value="'.$itemCat.'"/>
					<div id="sliderCat">
						<input type="text" id="newCategory" name="category_New"/><div id="bkbtn" title="Back to Categories"/></div>
					</div>
				</div>
				<div class="grid_3 omega bold">
					Description:
					<textarea id="descArea" name="description">'.$itemDesc.'</textarea>
				</div>
			</div>
		</div>
		<div id="extraSpace" class="grid_6 omega">
			&nbsp;
		</div>
		<div class="clear">
		<!--spanner-->
		</div>
		<div class="grid_6 alpha">
			<div class="grid_6 alpha bordered widgetDiv">
				<div class="grid_6 alpha bold">
						Notes:<br />
						<textarea id="notesArea" name="notes">'.$itemnotes.'</textarea>			
				</div>
			</div>
		</div>
		<div id="extraSpace2" class="grid_6 omega">
			&nbsp;
		</div>
		<div class="clear">
		<!--spanner-->
		</div>
		<div class="grid_6 alpha">
			<div class="grid_6 alpha centerAlign">
				<input type="submit" value="'.$acctBtn.'" class="buttons"/>
			</div>
		</div>
		<div id="extraSpace3" class="grid_6 omega">
			&nbsp;
		</div>
	</div>
	</form>
	';
}
else
{ 

//----PROCESSING
$ifExists = checkIfExists('inv_item', post('itemID'), 'itemID', 'checker');

if($ifExists != 'DB Query success!')
{
	$category = 'exisiting';
	if(post('category') == 'newCat')
	{
		if(post('category_New') != '')
		{ 
			
			$check = checkIfExists('inv_category', post('category_New'), 'catName', 'checker');
			if($check != 'DB Query success!')
			{
				$tableName = 'inv_category';
		
				$fields = array();
				$fields[0] = 'catName';
				$fields[1] = 'dateCreated';
				
				$values = array();
				$values[0] = post('category_New');
				$values[1] = sqlDateTime();
				
				$data2 = SQLInsert($tableName, $fields, $values);
			}
			$category = checkIfExists('inv_category', post('category_New'), 'catName', 'catID');

			$tableName = 'inv_item';
	
			$fields = array();
			$fields[0] = 'itemID';
			$fields[1] = 'itemDesc';
			$fields[2] = 'category';
			$fields[3] = 'notes';
			$fields[4] = 'created';
			$fields[5] = 'active';
			
			$values = array();
			$values[0] = post('itemID');
			$values[1] = post('description');
			$values[2] = $category;
			$values[3] = post('notes');
			$values[4] = sqlDateTime();
			$values[5] = '0';
			
			$data = SQLInsert($tableName, $fields, $values);		
			echo $data['queryStatus'];	
		}
	}
	else
	{

		$tableName = 'inv_item';

		$fields = array();
		$fields[0] = 'itemID';
		$fields[1] = 'itemDesc';
		$fields[2] = 'category';
		$fields[3] = 'notes';
		$fields[4] = 'created';
		$fields[5] = 'active';
		
		$values = array();
		$values[0] = post('itemID');
		$values[1] = post('description');
		$values[2] = post('category');
		$values[3] = post('notes');
		$values[4] = sqlDateTime();
		$values[5] = '0';
		
		$data = SQLInsert($tableName, $fields, $values);
		echo post('category');
	}
	$display = '<div class="grid_12 mainBox">
		<div class="grid_12 alpha">
			<h1>Item Account Properties</h1>
		</div>
		<div class="clear">
		<!--spanner-->
		</div>
		<div class="grid_6 alpha bordered widgetDiv">
			A new Item Account has been created. Close this window and refresh the Item Accounts window to manage your new account.
		</div>
		<div id="extraspace4" class="grid_6 omega">
		&nbsp;
		</div>
		<div class="clear">
		<!--spanner-->
		</div>
		<div class="grid_6 alpha centerAlign">
			<input type="button" class="buttons" value="Close" title="'.INDEX.'items'.EXT.'view=items" onClick="'.jsClose().'"/>
		</div>
		<div id="extraspace4" class="grid_6 omega">
			&nbsp;
		</div>
';
}
else
{
	$category = 'exisiting';
	if(get('id') != 'null')
	{
		if(post('category') == 'newCat')
		{
			if(post('category_New') != '')
			{ 
				
				$check = checkIfExists('inv_category', post('category_New'), 'catName', 'checker');
				if($check != 'DB Query success!')
				{
					$tableName = 'inv_category';
			
					$fields = array();
					$fields[0] = 'catName';
					$fields[1] = 'dateCreated';
					
					$values = array();
					$values[0] = post('category_New');
					$values[1] = sqlDateTime();
					
					$data2 = SQLInsert($tableName, $fields, $values);
				}
				$category = checkIfExists('inv_category', post('category_New'), 'catName', 'catID');
				
				$tableName = 'inv_item';
		
				$fields = array();
				$fields[0] = 'itemID';
				$fields[1] = 'itemDesc';
				$fields[2] = 'category';
				$fields[3] = 'notes';
				$fields[4] = 'created';
/* 				$fields[5] = 'count'; */
				$fields[5] = 'active';
				
				$values = array();
				$values[0] = post('itemID');
				$values[1] = post('description');
				$values[2] = $category;
				$values[3] = post('notes');
				$values[4] = sqlDateTime();
				$values[5] = '0';
				
				$whereClause = 'itemID="'.post('itemID').'";';
				
				$data = SQLUpdate($tableName, $whereClause, $fields, $values);
				if(get('lastEntry') == 'yes')
				{
					$checkLoadedCat = countItems(post('prevCat'));
					if($checkLoadedCat == 0)
					{
						$data = SQLDelete ('inv_category', 'catID="'.post('prevCat').'"');
					}
				}
			}
		}
		else
		{
			$tableName = 'inv_item';
	
			$fields = array();
			$fields[0] = 'itemID';
			$fields[1] = 'itemDesc';
			$fields[2] = 'category';
			$fields[3] = 'notes';
			$fields[4] = 'created';
/* 			$fields[5] = 'count'; */
			$fields[5] = 'active';
			
			$values = array();
			$values[0] = post('itemID');
			$values[1] = post('description');
			$values[2] = post('category');
			$values[3] = post('notes');
			$values[4] = sqlDateTime();
			$values[5] = '0';
/* 			$values[6] = '0'; */
			
			$whereClause = 'itemID="'.post('itemID').'";';
			
			$data = SQLUpdate($tableName, $whereClause, $fields, $values);
			if(get('lastEntry') == 'yes')
			{
				$checkLoadedCat = countItems(post('prevCat'));
				if($checkLoadedCat == 0)
				{
					$data = SQLDelete ('inv_category', 'catID="'.post('prevCat').'"');
				}
			}
		}
		$display = '<div class="grid_12 mainBox">
		<div class="grid_12 alpha">
			<h1>Item Account Properties</h1>
		</div>
		<div class="clear">
		<!--spanner-->
		</div>
		<div class="grid_6 alpha bordered widgetDiv">
			You have provided an Item Code ('.post('itemID').') that already exists in the inventory system, that account has been updated. Click this <a href="'.INDEX.'control'.EXT.'sector=newItem" id="backlink">link</a> if you wish to go back and create a new one.
		</div>
		<div id="extraspace4" class="grid_6 omega">
		&nbsp;
		</div>
		<div class="clear">
		<!--spanner-->
		</div>
		<div class="grid_6 alpha centerAlign">
			<input type="button" class="buttons" value="Close" title="'.INDEX.'items'.EXT.'view=items" onClick="'.jsClose().'"/>
		</div>
		<div id="extraspace4" class="grid_6 omega">
			&nbsp;
		</div>
';
	}
	else
	{
		$display = '<div class="grid_12 mainBox">
		<div class="grid_12 alpha">
			<h1>Item Account Properties</h1>
		</div>
		<div class="clear">
		<!--spanner-->
		</div>
		<div class="grid_6 alpha bordered widgetDiv">
			You have provided an Item Code ('.post('itemID').') that already exists in the inventory system, that account has not been updated. Click this <a href="'.INDEX.'control'.EXT.'sector=newItem" id="backlink">link</a> if you wish to go back and create a new one.
		</div>
		<div id="extraspace4" class="grid_6 omega">
		&nbsp;
		</div>
		<div class="clear">
		<!--spanner-->
		</div>
		<div class="grid_6 alpha centerAlign">
			<input type="button" class="buttons" value="Close" title="'.INDEX.'items'.EXT.'view=items" onClick="'.jsClose().'"/>
		</div>
		<div id="extraspace4" class="grid_6 omega">
			&nbsp;
		</div>
';
	}
	
}
}

?>